package net.url;

import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

public class DaytimeURLConnection extends URLConnection
{
	private Socket connection = null;
	public final static int DEFAULT_PORT =13;
	public DaytimeURLConnection(URL u)
	{
		super(u);
	}
	@Override
	public synchronized InputStream getInputStream() throws IOException
	{
		if(!connected)
		{
			connect();
		}
		InputStream in = this.connection.getInputStream();
		SequenceInputStream result = new SequenceInputStream(in,null);
		return result;
	}
	@Override
	public String getContentType()
	{
		return "text/plain";
	}
	@Override
	public synchronized void connect() throws IOException
	{
		if(!connected)
		{
			int port = url.getPort();
			if(port<=0||port>65535)
			{
				port = DEFAULT_PORT;
			}
			this.connection = new Socket(url.getHost(), port);
			this.connected = true;
		}
	}
}
